﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using BUL;
using System.Data;
namespace Hospital
{
    public partial class StaffAllocation : System.Web.UI.Page
    {
        StaffBUL staff = new StaffBUL();
        ClinicBUL clinic = new ClinicBUL();
        WardBUL ward = new WardBUL();
        DivisionBUL divBUL = new DivisionBUL();
        DivisionDAO divDAO = new DivisionDAO();
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadId();
                reset();
            }
        }
        private void loadId()
        {
            dt = staff.loadId();
            listId.DataSource = dt;
            listId.DataTextField = "idStaff";
            listId.DataValueField = "idStaff";
            listId.DataBind();
            dt.Clear();
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            divDAO.Date = DateTime.Parse(txtDate.Text);
            divDAO.Date.ToString("dd/mm/yyyy");
            divDAO.IdStaff = Int32.Parse(listId.Text);
            if (rdWard.Checked)
            {
                divDAO.IdWard = Int32.Parse(list.SelectedValue.ToString());
                divDAO.IdClinic = 0;
            }
            else
            {
                divDAO.IdWard = 0;
                divDAO.IdClinic = Int32.Parse(list.SelectedValue.ToString());
            }
            divBUL.insert(divDAO);
            reset();
            load();
        }

        protected void listId_TextChanged(object sender, EventArgs e)
        {
               txtName.Text = staff.getFullName(listId.SelectedValue.ToString());
               txtPosition.Text = staff.getPositionById(listId.SelectedValue.ToString());
               rdWard.Enabled = true; 
               list.Enabled = true;
               if (txtPosition.Text.Equals("Bác sĩ"))
               {
                   rdClinic.Enabled = true;
               }
        }
        protected void calEventDate_SelectionChanged(object sender, EventArgs e)
        {
            txtDate.Text = calEventDate.SelectedDate.ToString("yyyy-MM-dd");
            load();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!txtId.Text.Equals(""))
            {
                divBUL.delete(txtId.Text);
                load();
                reset();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            divDAO.IdDivision = Int32.Parse(txtId.Text);
            divDAO.Date = DateTime.Parse(txtDate.Text);
            divDAO.Date.ToString("dd/mm/yyyy");
            divDAO.IdStaff = Int32.Parse(listId.Text);
            if (rdWard.Checked)
            {
                divDAO.IdWard = Int32.Parse(list.SelectedValue.ToString());
                divDAO.IdClinic = 0;
            }
            else {
                divDAO.IdWard = 0;
                divDAO.IdClinic = Int32.Parse(list.SelectedValue.ToString());
            }
            divBUL.update(divDAO);
            reset();
            load();
        }

        protected void txtReset_Click(object sender, EventArgs e)
        {
            reset();
        }
        private void reset() {
            txtId.Text = "";
            loadId();
            txtName.Text = "";
            txtPosition.Text = "";
            rdClinic.Enabled = false;
            rdWard.Enabled = false;
            rdWard.Checked = false;
            rdClinic.Checked = false;
            list.Dispose();
        }
        protected void grvDivision_SelectedIndexChanged(object sender, EventArgs e)
        {
            rdWard.Checked = false;
            rdClinic.Checked = false;
            rdClinic.Enabled = false;
            rdWard.Enabled = false;
            list.Enabled = false;
            txtId.Text =HttpUtility.HtmlDecode(grvDivision.SelectedRow.Cells[1].Text);
            setItem(listId,HttpUtility.HtmlDecode(grvDivision.SelectedRow.Cells[2].Text));
            txtName.Text =HttpUtility.HtmlDecode(grvDivision.SelectedRow.Cells[3].Text);
            txtPosition.Text = HttpUtility.HtmlDecode(grvDivision.SelectedRow.Cells[4].Text);
            if (txtPosition.Text.Equals("Bác sĩ"))
            {
                rdClinic.Enabled = true;
                rdWard.Enabled = true;
            }
            else {
                rdWard.Enabled = true;
            }
            list.Enabled = true;
            if (!grvDivision.SelectedRow.Cells[5].Text.Equals("------------------"))
            {
                rdClinic.Checked = true;
                rdClinic_CheckedChanged(sender, e);
                setItem(list, HttpUtility.HtmlDecode(grvDivision.SelectedRow.Cells[5].Text)); 
            }
            else {
                rdWard.Checked = true;
                rdWard_CheckedChanged(sender, e);
                setItem(list, HttpUtility.HtmlDecode(grvDivision.SelectedRow.Cells[6].Text));
            }
        }
        private void setItem(DropDownList list,String name) {
            list.SelectedIndex = list.Items.IndexOf(list.Items.FindByText(name));
        }
        protected void txtDate_TextChanged(object sender, EventArgs e)
        {
            load();
        }
        private void load() {
            grvDivision.DataSource = divBUL.load(txtDate.Text);
            grvDivision.DataBind();
        }

        protected void rdClinic_CheckedChanged(object sender, EventArgs e)
        {
            if (rdClinic.Checked)
            {
                dt = clinic.load();
                list.DataSource = dt;
                list.DataTextField = "name";
                list.DataValueField = "idClinic";
                list.DataBind();
                dt.Clear();
            }
        }

        protected void rdWard_CheckedChanged(object sender, EventArgs e)
        {
            if (rdWard.Checked)
            {
                dt = ward.load();
                list.DataSource = dt;
                list.DataTextField = "name";
                list.DataValueField = "idWard";
                list.DataBind();
                dt.Clear();
            }
        }

    
    }
}